//
//  MMapViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/10/28.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MMapViewController.h"
#import "MDDataBaseUtil.h"
#import "MListModel.h"
#import "MListServices.h"
#import "MMerchantViewController.h"
@interface MMapViewController ()
@property (nonatomic, strong) MMerchantViewController *merchantVC;
@end

@implementation MMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0,0, SCREENWIDTH, SCREENHEIGHT-100)];
    self.view = mapView;
    self.merchantMutDict = [NSMutableDictionary dictionary];
    self.annotionsArray = [NSMutableArray array];
    [self mListData];
    
    
}
- (void)viewDidAppear:(BOOL)animated {
    if (self.annotionsArray) {// 原来地图上有的 将它删除掉
        [mapView removeAnnotations:self.annotionsArray];
    }
    if (self.merchantMutDict) {
        [self addMerchantsAnnotionToMapWithDic:self.merchantMutDict];
    }
    
}
- (void)mListData {
    [[MListServices sharedClient]getProductListWithKid:@"0"
                                              latitude:[MDDataBaseUtil txtLatitude]
                                            longtitude:[MDDataBaseUtil txtLongtitude]
                                                radius:@"10000"
                                                  sort:@"Price"
                                                typeId:nil
                                               priceId:nil
                                                sizeId:nil
                                             pageIndex:@"0"
                                            completion:^(NSMutableArray *result) {
                                                
                                                if (result.count == 0) {
                                                    NSLog(@"没有更多数据了");
                                                } else {
                                                    for (MListModel *model in result) {
                                                        
                                                        
                                                        [self.merchantMutDict setObject:model forKey:model.companyName];
                                                    }
                                                    [self addMerchantsAnnotionToMapWithDic:self.merchantMutDict];   
                                                }
                                            }
                                               failure:^(NSString *error){
                                                   
                                                   NSLog(@"failure");
                                               }];
}
- (void)addMerchantsAnnotionToMapWithDic:(NSDictionary *)dic{

    for (NSString *key in dic) {
        MListModel *model = dic[key];
        BMKPointAnnotation *pointAnnotion = [[BMKPointAnnotation alloc]init];
        CLLocationCoordinate2D coor = CLLocationCoordinate2DMake([model.latitude floatValue], [model.longtitude floatValue]);
        pointAnnotion.coordinate = coor;
        pointAnnotion.title = model.companyName;
        [self.annotionsArray addObject:pointAnnotion];
        [mapView addAnnotation:pointAnnotion];
        [mapView setCenterCoordinate:coor animated:YES];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [mapView viewWillAppear];
    mapView.delegate = self;
}
-(void)viewDidDisappear:(BOOL)animated{
    
    [mapView removeAnnotations:self.annotionsArray];
    [mapView viewWillDisappear];
    mapView.delegate = nil;
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view {

    if ([view.annotation isKindOfClass:[BMKPointAnnotation class]]) {
        NSString *merchantKey = ((BMKPointAnnotation *)view.annotation).title;
        MListModel *model = self.merchantMutDict[merchantKey];
        
        self.merchantVC = [[MMerchantViewController alloc]init];
        self.merchantVC.model = model;
        self.merchantVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:self.merchantVC animated:YES completion:nil];
    }
    
    
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation {

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        for (BMKPointAnnotation *point in self.annotionsArray) {
            if ([self.selectedTitle isEqualToString:point.title]) {
                newAnnotationView.pinColor = BMKPinAnnotationColorGreen;
            }else {
            
                newAnnotationView.pinColor = BMKPinAnnotationColorRed;
            }
        }
        return newAnnotationView;
    }
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
